package phase01.module4.code.question4;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    private ServerSocket serverSocket;

    public Server(int port) {
        try {
            serverSocket = new ServerSocket(port);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("InfiniteLoopStatement")
    public void start() {
        System.out.println("服务器已启动！");
        System.out.println("==============================================================================================");
        Socket socket;
        while (true) {
            try {
                socket = serverSocket.accept();
                System.out.println("客户端" + socket.getInetAddress() + "连接成功！");
                ServiceThread serviceThread = new ServiceThread(socket);
                serviceThread.start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
